﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections.Generic;

public class user
{
    private long id;
    private string nn;//nackname;
    private string us;//username;
    private string em;//email;
    private string ps;//password;
    private string im;//image, icon;
    private string dct;//dictum
    private string crtm;
    private string cc;// it's check code;
    private string lt;//the time the current user logined in.
    private string searchEngine;
    private IEnumerable<XElement> element;
    private XElement root;
    private string filename;
	public user(string filename)
	{
        this.filename = filename;
        root = XElement.Load(filename);
        try
        {
            id = Convert.ToInt64(root.Element("ID").Value);
            us = root.Element("us").Value;
            em = root.Element("em").Value;
            ps = root.Element("ps").Value;
            im = root.Element("im").Value;
            dct = root.Element("dct").Value;
            crtm = root.Element("crtm").Value;
            if (root.Element("cc") == null)
            {
                root.Add(new XElement("cc",""));
            }
            cc = root.Element("cc").Value;
            if (root.Element("lt") == null)
            {
                root.Add(new XElement("lt", ""));
            }
            lt = root.Element("lt").Value;
            if (root.Element("searchEngine") == null)
            {
                root.Add(new XElement("searchEngine", "baidu"));
            }
            searchEngine = root.Element("searchEngine").Value;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
        }
    }

    public user()
    {
    }

    public long ID
    {
        get
        {
            return this.id;
        }
        set
        {
            this.id = value;
        }
    }

    public string Us
    {
        get
        {
            return this.us;
        }
        set
        {
            this.us = value;
        }
    }

    public string Em
    {
        get
        {
            return this.em;
        }
        set
        {
            this.em = value;
        }
    }

    public string Ps
    {
        get
        {
            return this.ps;
        }
        set
        {
            this.ps = value;
        }
    }

    public string Crtm //the creating time
    {
        get;
        set;
    }

    public string Im
    {
        get
        {
            return this.im;
        }
        set
        {
            this.im = value;
        }
    }

    public string Dct
    {
        get
        {
            return this.dct;
        }
        set
        {
            this.dct = value;
        }
    }
    public string Cc
    {
        get
        {
            return this.cc;
        }
        set
        {
            this.cc = value;
        }
    }
    public string Lt
    {
        get
        {
            return this.lt;
        }
        set
        {
            this.lt = value;
        }
    }
    public string SearchEngine
    {
        get
        {
            return this.searchEngine;
        }
        set
        {
            this.searchEngine = value;
        }
    }

    public XElement Root
    {
        set
        {
            root = value;
        }
        get
        {
            return root;
        }
    }

    public void getAllValue()
    {  
        us  = element.Elements("us").First().Value;
        em = element.Elements("em").First().Value;
        ps = element.Elements("ps").First().Value;
        im  = element.Elements("im").First().Value;
        dct = element.Elements("dct").First().Value;
        crtm = element.Elements("crtm").First().Value;
        cc = element.Elements("cc").First().Value;
        lt = element.Elements("lt").First().Value;
        searchEngine = element.Elements("searchEngine").First().Value;
    }

    public void setAllValue()
    {
        root.Element("us").Value = us;
        root.Element("em").Value = em;
        root.Element("ps").Value = ps;
        root.Element("im").Value = im;
        root.Element("dct").Value = dct;
        root.Element("crtm").Value = crtm;
        root.Element("cc").Value = cc;
        root.Element("lt").Value = lt;
        root.Element("searchEngine").Value = searchEngine;
    }

    public bool saveToFile()
    {
        try
        {
            root.Save(this.filename);
            return true;
        }
        catch(Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }
    }

    public bool saveToFile(string filename)
    {
        try
        {
            root.Save(filename);
            return true;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }
    }
}
